classdef GridLayoutEx < handle
    properties
        grid
    end
    
    methods
        function obj = GridLayoutEx(parent, sz, varargin)
            obj.grid = uigridlayout(parent, sz, varargin{:});
        end
        
        function ui = placeUI(obj, uinameStr, row, column, varargin)
            p=inputParser;
            p.addRequired('uinameStr', @(x) ismember(lower(x), {'uibutton', 'uitable', 'uipanel', 'uidropdown', 'uilistbox', 'uitree', 'uilabel'}));
            p.addRequired('row', @(x) isnumeric(x) && all(x>0));
            p.addRequired('column', @(x) isnumeric(x) && all(x>0));
            p.parse(uinameStr, row, column);
            
            ui = eval(sprintf('%s(obj.grid)', uinameStr));
            ui.Layout.Row = row;
            ui.Layout.Column = column;
            if ~isempty(varargin)
                set(ui, varargin{:});
            end
        end
        
        function sg = subGrid(obj, row, column, sz, varargin)
            p=inputParser;            
            p.addRequired('row', @(x) isnumeric(x) && all(x>0));
            p.addRequired('column', @(x) isnumeric(x) && all(x>0));
            p.addRequired('sz', @(x) isnumeric(x) && all(x>0) && length(x) == 2);
            p.parse(row, column, sz);
            
            sg = Miscellaneous.GUICreator.GridLayoutEx(obj.grid, sz, varargin{:});
            sg.grid.Layout.Row = row;
            sg.grid.Layout.Column = column;
        end
        
        function [ui, subgrid, panel] = placeUI_in_Panel(obj, row, column, text, uinameStr, varargin)
            [subgrid, panel] = obj.subGrid_in_Panel(row, column, [1 1], text, 'Padding', [0 0 0 0]);
            ui = eval(sprintf('subgrid.placeUI("%s", 1, 1)', uinameStr));
            if ~isempty(varargin)
                set(ui, varargin{:});
            end
        end
        
        function [subgrid, panel] = subGrid_in_Panel(obj, row, column, sz, text, varargin)
            panel = obj.placeUI('uipanel', row, column, 'Title', text);
            subgrid = Miscellaneous.GUICreator.GridLayoutEx(panel, sz, varargin{:});
        end
    end
end

